package com.h3c.gm.service

import com.google.common.collect.Lists
import com.google.common.collect.Maps
import com.h3c.gm.dao.IBusOrgDao
import com.h3c.gm.dao.ISysOrgQuotaDao
import com.h3c.gm.entity.DO.SysOrgQuotaDO
import org.springframework.stereotype.Service

import javax.annotation.Resource
import java.text.DecimalFormat

/**
 * Created by d14287 on 2017/6/26.
 */
@Service
class SysOrgQuotaService {
    @Resource
    ISysOrgQuotaDao sysOrgQuotaDao
    @Resource
    IBusOrgDao busOrgDao

    def getOrgQuota(String orgkey, String qtype) {
        def queryParams = Maps.newHashMap()
        queryParams.put('orgkey', orgkey)
        queryParams.put('qtype', qtype)
        def sysOrgQuotaDO = sysOrgQuotaDao.getOrgQuota(queryParams)
    }

    def insert(SysOrgQuotaDO sysOrgQuotaDO) {
        sysOrgQuotaDao.insert(sysOrgQuotaDO)
    }

    void deleteByOrgkey(String orgkey) {
        sysOrgQuotaDao.deleteByOrgkey(orgkey)
    }

    Map<String, Object> getStarageStatistics() {
        def map = Maps.newHashMap()
        Long capacity = 0L
        Long usedCapacity = 0L
        Map<String, Object> count = sysOrgQuotaDao.getCount(Maps.newHashMap())
        map.put('count', count.get('sum_quotacount'))
        //总个数
        capacity = count.get('sum_quotacount')
        map.put('capacity', capacity * 500)
        //已分配
        map.put('usedCapacity', usedCapacity = count.get('sum_quotaused'))
        map.put('unUsedCapacity', capacity.longValue() - usedCapacity.longValue())
        map.put('ratio', capacity == 0L ? 0 : new DecimalFormat('#.00').format(usedCapacity.longValue() / capacity.longValue()))
        return map
    }

    Map<String, Object> getProStarageStatistics(String province) {
        def orgkeyList = getOrgkeyList(province)
        def queryParams = Maps.newHashMap()
        queryParams.put('orgkeyList', orgkeyList)
        def map = Maps.newHashMap()
        Long capacity = 0L
        Long usedCapacity = 0L
        Map<String, Object> count = sysOrgQuotaDao.getCount(queryParams)
        map.put('count', count.get('sum_quotacount'))
        //总个数
        map.put('capacity', capacity = count.get('sum_quotacount'))
        //已分配
        map.put('usedCapacity', usedCapacity = count.get('sum_quotaused'))
        map.put('unUsedCapacity', capacity.longValue() - usedCapacity.longValue())
        map.put('ratio', capacity == 0L ? 0 : new DecimalFormat('#.00').format(usedCapacity.longValue() / capacity.longValue()))
        return map
    }

    List<String> getOrgkeyList(String province) {
        def busOrgDOs = busOrgDao.getCityOrg(province)
        def orgkeys = Lists.newArrayList()
        busOrgDOs.each {
            item ->
                orgkeys.add(item.getOrgkey())
        }
        return orgkeys
    }
}
